﻿
namespace personalplaner.business.extensions
{
	public static class ColorExtensions
	{
		/// <summary>
		/// Converts a int value to a color
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Windows.Media.Color? ToColor(this int value)
		{
			if (value == -1 || value == 0)
				return null;

			var color = System.Drawing.Color.FromArgb(value);
			return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
		}

		/// <summary>
		/// Converts a color to a int
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static int ToInt(this System.Windows.Media.Color? value)
		{
			if (value == null)
				return -1;

            if (value == System.Windows.Media.Colors.White || value == System.Windows.Media.Colors.Transparent)
                return -1;

			var color = (System.Windows.Media.Color)value;
			return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B).ToArgb();
		}

		/// <summary>
		/// Converts a color to a int
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static int ToInt(this System.Windows.Media.Color value)
		{
            if (value == System.Windows.Media.Colors.White ||
                value == System.Windows.Media.Colors.Transparent)
                return -1;

			return System.Drawing.Color.FromArgb(value.A, value.R, value.G, value.B).ToArgb();
		}

		/// <summary>
		/// Converts a comma separated string containing the ARGB values to a color
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Windows.Media.Color? ToColor(this string value)
		{
			if (string.IsNullOrEmpty(value))
				return null;

			var arr = value.Split(',');

			if (arr.Length == 3)
			{
				byte a = 0;
				byte r = byte.Parse(arr[0]);
				byte g = byte.Parse(arr[1]);
				byte b = byte.Parse(arr[2]);
				return System.Windows.Media.Color.FromArgb(a, r, g, b);
			}
			else if (arr.Length == 4)
			{
				byte a = byte.Parse(arr[0]);
				byte r = byte.Parse(arr[1]);
				byte g = byte.Parse(arr[2]);
				byte b = byte.Parse(arr[3]);
				return System.Windows.Media.Color.FromArgb(a, r, g, b);
			}

			return null;
		}

		/// <summary>
		/// Converts a color to a comma separated string containing the ARGB values
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string ToArgbString(this System.Windows.Media.Color? value)
		{
			if (value == null || value == System.Windows.Media.Colors.Transparent)
				return "-1";

			var color = (System.Windows.Media.Color)value;
			return string.Format("{0},{1},{2},{3}", color.A, color.R, color.G, color.B);
		}

        /// <summary>
        /// Converts a color to a comma separated string containing the ARGB values
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToArgbString(this System.Windows.Media.Color value)
        {
            if (value == null || value == System.Windows.Media.Colors.Transparent)
                return "-1";

            return string.Format("{0},{1},{2},{3}", value.A, value.R, value.G, value.B);
        }

        /// <summary>
        /// checks if a color is white or transparent
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmptyColor(this System.Windows.Media.Color value)
        {
            if (value == null || value == System.Windows.Media.Colors.Transparent || value == System.Windows.Media.Colors.White)
                return true;

            if (value.A == 0 && value.R == 0 && value.G == 0 && value.B == 0)
                return true;

            return false;
        }
	}
}
